package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 李旭
 * @date 2022/1/12 14:34
 * @Description:
 */
@Controller
public class ListController {


    @Autowired
    private ListFeignClient listFeignClient;

    //搜索  http://list.gmall.com/list.html  ?keyword=手机&brand=..
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model, HttpServletRequest request) {

        System.out.println(request.getRequestURI());
        System.out.println(request.getRequestURL().toString());
        SearchResponseVo vo = listFeignClient.list(searchParam);//远程调用
        //1:页面传递的参数对象进行回显
        model.addAttribute("searchParam", searchParam);
        //2:品牌集合
        model.addAttribute("trademarkList", vo.getTrademarkList());
        //3:平台属性集合
        model.addAttribute("attrsList", vo.getAttrsList());
        //4:商品集合
        model.addAttribute("goodsList", vo.getGoodsList());
        //5:分页
        model.addAttribute("totalPages", vo.getTotalPages());
        model.addAttribute("pageNo", vo.getPageNo());
        //6: url + param 拼接字段
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        //7:为了排序  样式  箭头 等 orderMap
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);
        //8:回显  选中品牌的回显
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam", trademarkParam);

        //9:回显  选中平台属性的回显
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList", propsParamList);

        return "list/index";
    }

    //回显  选中平台属性的回显
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        //props=23:4G:运行内存
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            return Arrays.stream(props).map(prop -> {
                String[] p = prop.split(":");
                Map map = new HashMap();
                map.put("attrId",p[0]);
                map.put("attrValue",p[1]);
                map.put("attrName",p[2]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //回显  选中品牌的回显
    private String buildTrademarkParam(SearchParam searchParam) {
        //trademark=2:华为
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            String[] t = trademark.split(":");
            return "品牌:" + t[1];
        }
        return null;
    }

    //为了排序  样式  箭头 等 orderMap
    private Map buildOrderMap(SearchParam searchParam) {
        Map orderMap = new HashMap();
        // order=1:asc  排序规则   0:asc
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)) {
            String[] o = order.split(":");
            orderMap.put("type", o[0]);
            orderMap.put("sort", o[1]);
        } else {
            //默认值  综合 == 热度分  高到低
            orderMap.put("type", "1");
            orderMap.put("sort", "desc");
        }
        return orderMap;
    }

    //url + param 拼接字段
    private String buildUrlParam(SearchParam searchParam) {

        StringBuilder sb = new StringBuilder();// 线程不安全： 无锁
        sb.append("http://list.gmall.com/list.html?");
        //1:关键词
        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)) {
            sb.append("keyword=").append(keyword);
        }
        //2:品牌
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            sb.append("&trademark=").append(trademark);
        }
        //3:平台属性  http://list.gmall.com/list.html?keyword=rrrr&props=3:8G:运行内存&&props=。。。
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        return sb.toString();
    }
}
